package com.wjj.happytime.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wjj.happytime.R;
import com.wjj.happytime.fragment.main_frg.GameFragment;
import com.wjj.happytime.fragment.main_frg.HappyFragment;
import com.wjj.happytime.fragment.main_frg.ListenFragment;
import com.wjj.happytime.fragment.main_frg.LiveFragment;

import java.util.LinkedList;

/**
*
*
* @author Wang_JunJie
* @date 2017/1/9
*
*/
public class MainActivity extends AppCompatActivity {

    private FrameLayout container;
    private RadioGroup radioGroup;
    private LinkedList<Fragment> frgs;
    private HappyFragment happyFragment;
    private ListenFragment listenFragment;
//    private LiveFragment liveFragment;
    private GameFragment gameFragment;
    private FragmentManager fragmentManager;
    private Fragment currentFrg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //关于radioGroup
        aboutRadioGroup();

    }
    //关于radioGroup
    private void aboutRadioGroup() {
       //获取fragmentManager
        fragmentManager = getSupportFragmentManager();
        //给radioGroup设置监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    if (radioButton.isChecked()) {
                        changeContainerWidget(i);
                        break;
                    }
                }
            }
        });
        //界面启动 默认第一个选中
        currentFrg = frgs.get(0);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        ((RadioButton) radioGroup.getChildAt(0)).setTextColor(getResources().getColor(R.color.colorMainText));
    }

    //修改占位的容器控件
    private void changeContainerWidget(int i) {
        FragmentTransaction beginTransaction = fragmentManager
                .beginTransaction();
        // 1、隐藏当前的
        beginTransaction.hide(currentFrg);
        //设置字体为黑
        for (int j = 0; j < radioGroup.getChildCount(); j++) {
            if (j!=i) {
                ((RadioButton) radioGroup.getChildAt(j)).setTextColor(0XFF000000);
            }
        }


        // 2、判断选中的RadioButton对应的Fragment是否曾经附着在容器控件上
        Fragment nowFrament = frgs.get(i);
        // ①附着上了，就显示
        // ②没有附着，先附着，再显示
        if (!nowFrament.isAdded()) {
            beginTransaction.add(R.id.container_main_id, nowFrament);
        }
        beginTransaction.show(nowFrament);
        ((RadioButton) radioGroup.getChildAt(i)).setTextColor(getResources().getColor(R.color.colorMainText));

        // 3、将目前选中Fragment赋值给当前currentFragment
        currentFrg = nowFrament;

        // 4、commit
        beginTransaction.commit();
    }

    //初始化控件
    private void initView() {
        container = ((FrameLayout) findViewById(R.id.container_main_id));
        radioGroup = ((RadioGroup) findViewById(R.id.radio_group_id));

        //准备一个集合来存放Fragment
        frgs = new LinkedList<>();
        //创建frg 并添加进集合
        happyFragment = new HappyFragment();
        listenFragment = new ListenFragment();
//        liveFragment = new LiveFragment();
        gameFragment = new GameFragment();
        frgs.add(happyFragment);
        frgs.add(listenFragment);
//        frgs.add(liveFragment);
        frgs.add(gameFragment);
    }
}
